package com.msb.mall.producet.service.impl;

import com.msb.mall.producet.entity.BrandEntity;
import com.msb.mall.producet.entity.SkuImagesEntity;
import com.msb.mall.producet.entity.SpuInfoDescEntity;
import com.msb.mall.producet.service.*;
import com.msb.mall.producet.vo.SkuItemSaleAttrVo;
import com.msb.mall.producet.vo.SpuItemGroupAttrVo;
import com.msb.mall.producet.vo.SpuItemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.msb.common.utils.PageUtils;
import com.msb.common.utils.Query;

import com.msb.mall.producet.dao.SkuInfoDao;
import com.msb.mall.producet.entity.SkuInfoEntity;
import org.springframework.util.StringUtils;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Autowired
    SkuInfoDao skuInfoDao;

    @Autowired
    SkuImagesService skuImagesService;

    @Autowired
    SpuInfoDescService spuInfoDescService;

    @Autowired
    AttrGroupService attrGroupService;

    @Autowired
    SkuSaleAttrValueService skuSaleAttrValueService;

    @Autowired
    BrandService brandService;

    @Autowired
    ThreadPoolExecutor threadPoolExecutor;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }


    /**
     * sku信息条件查询
     * 类别  品牌  价格区间  关键字  分页查询
     *
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<>();

        //关键字
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            wrapper.and(
                    w -> w.eq("sku_id", key).or().like("sku_name", key)
            );
        }

        //分类
        String catelogId = (String) params.get("catelogId");
        if (!StringUtils.isEmpty(catelogId) && !"0".equalsIgnoreCase(catelogId)) {
            wrapper.eq("catalog_id", catelogId);
        }

        //品牌
        String brandId = (String) params.get("brandId");
        if (!StringUtils.isEmpty(brandId) && !"0".equalsIgnoreCase(brandId)) {
            wrapper.eq("brand_id", brandId);
        }

        //价格区间
        String min = (String) params.get("min");
        String max = (String) params.get("max");
        if (!StringUtils.isEmpty(min)) {
            wrapper.ge("price", min);
        }
        if (!StringUtils.isEmpty(max)) {
            try {
                //如果max等于0  就不需要加这个条件
                BigDecimal bigDecimal = new BigDecimal(max);
                if (bigDecimal.compareTo(new BigDecimal(0)) == 1) {
                    //说明max大于0
                    wrapper.le("price", max);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }


    /**
     * 根据spuId查询sku信息
     *
     * @param spuId
     * @return
     */
    @Override
    public List<SkuInfoEntity> getSkusBySpuId(Long spuId) {
        List<SkuInfoEntity> skus = this.list(new QueryWrapper<SkuInfoEntity>().eq("spu_id", spuId));
        return skus;
    }


    /**
     * 异步编排
     *
     * @param skuId
     * @return
     */
    @Override
    public SpuItemVO item(Long skuId) throws ExecutionException, InterruptedException {
        SpuItemVO vo = new SpuItemVO();
        CompletableFuture<SkuInfoEntity> skuInfoFuture = CompletableFuture.supplyAsync(() -> {
            //1. sku的基本信息  pms_sku_imfo
            SkuInfoEntity skuInfoEntity = getById(skuId);
            vo.setInfo(skuInfoEntity);

            Long brandId = skuInfoEntity.getBrandId();

            BrandEntity brand = brandService.getById(brandId);
            vo.setBrandName(brand.getName());
            return skuInfoEntity;
        }, threadPoolExecutor);

        CompletableFuture<Void> saleFuture = skuInfoFuture.thenAcceptAsync(result -> {
            //3. 获取spu中的销售属性的组合
            List<SkuItemSaleAttrVo> saleAttrs = skuSaleAttrValueService.getSkuSaleAttrValueBySpuId(result.getSpuId());
            vo.setSaleAttrs(saleAttrs);
        }, threadPoolExecutor);

        CompletableFuture<Void> spuFuture = skuInfoFuture.thenAcceptAsync(result -> {
            //4. 获取spu的介绍信息
            SpuInfoDescEntity spuInfoDescEntity = spuInfoDescService.getById(result.getSpuId());
            vo.setDesc(spuInfoDescEntity);
        }, threadPoolExecutor);

        CompletableFuture<Void> groupFuture = skuInfoFuture.thenAcceptAsync(result -> {
            //5. 获取spu的规格信息    --# 根据 SPU编号和CatalogId类别编号 要查询出所有的属性组及其属性信息
            List<SpuItemGroupAttrVo> groupAttrVo = attrGroupService.getAttrgroupWithSpuId(result.getSpuId(), result.getCatalogId());
            vo.setBaseAttrs(groupAttrVo);
        }, threadPoolExecutor);


        //和第一个并行
        CompletableFuture<Void> imageFuture = CompletableFuture.runAsync(() -> {
            //2. sku图片信息  pms_sku_images
            List<SkuImagesEntity> skuImagesEntities = skuImagesService.getImagesBySkuId(skuId);
            vo.setImages(skuImagesEntities);
        }, threadPoolExecutor);

        CompletableFuture.allOf(saleFuture,spuFuture,groupFuture,imageFuture).get();

        return vo;
    }

    /**
     * 获取销售属性 根据skuId
     * @param skuId
     * @return
     */
    @Override
    public List<String> getSkuSaleAttrs(Long skuId) {
        return this.skuInfoDao.getSkuSaleAttrs(skuId);
    }


    /*@Override
    public SpuItemVO item(Long skuId) {
        SpuItemVO vo = new SpuItemVO();
        //1. sku的基本信息  pms_sku_imfo
        SkuInfoEntity skuInfoEntity = getById(skuId);
        vo.setInfo(skuInfoEntity);
        Long spuId = skuInfoEntity.getSpuId();
        Long catalogId = skuInfoEntity.getCatalogId();
        Long brandId = skuInfoEntity.getBrandId();

        BrandEntity brand = brandService.getById(brandId);
        vo.setBrandName(brand.getName());


        //2. sku图片信息  pms_sku_images
        List<SkuImagesEntity> skuImagesEntities = skuImagesService.getImagesBySkuId(skuId);
        vo.setImages(skuImagesEntities);


        //3. 获取spu中的销售属性的组合
        List<SkuItemSaleAttrVo> saleAttrs= skuSaleAttrValueService.getSkuSaleAttrValueBySpuId(spuId);
        vo.setSaleAttrs(saleAttrs);

        //4. 获取spu的介绍信息
        SpuInfoDescEntity spuInfoDescEntity = spuInfoDescService.getById(spuId);
        vo.setDesc(spuInfoDescEntity);


        //5. 获取spu的规格信息    --# 根据 SPU编号和CatalogId类别编号 要查询出所有的属性组及其属性信息
        List<SpuItemGroupAttrVo> groupAttrVo = attrGroupService.getAttrgroupWithSpuId(spuId, catalogId);
        vo.setBaseAttrs(groupAttrVo);


        return vo;
    }*/

}